/*
 * @Author: wurangkun wurangkun@qq.com
 * @Date: 2023-07-07 09:05:56
 * @LastEditors: wurangkun wurangkun@qq.com
 * @LastEditTime: 2023-07-07 12:06:44
 * @FilePath: \reset_sever\shutdown.js
 * @Description: 
 */
const fs = require('fs')
const path = require('path');
const { exec } = require('child_process');
const filePath = path.join(__dirname, './data/pid.text');
// let count = +fs.readFileSync(filePath).toString('utf-8')
const notifier = require('node-notifier');
const { minutes } = require('../config');

/**
 * 关机
 */
const shutdown = () => {
  notifier.notify({
    title: '关机提示',
    message: `${minutes}分钟后自动关机：${process.pid}`,
    function(error, response) {
      console.log(response, error);
    }
  });

  setTimeout(() => {
    console.log('关机');
    fs.writeFileSync(filePath, '')
    exec('shutdown /s /t 0', (error, stdout, stderr) => {
      if (error) {
        console.error(`执行命令时出错：${error.message}`);
        return;
      }
      // 命令成功执行
      console.log(stdout);
    });
  }, 1000 * 60 * minutes);
}

module.exports = { shutdown };
